/**
 * 列表List
 */
def list1 = [1, 2, 3]
println list1.class
//list转数组
def array1 = list1 as int[]
int[] array2 = [4, 5, 6]

def list2 = [6, -1, 4, 2, 0, -9]
list2.sort()
println list2

list2.sort {
    x, y -> Math.abs(x) < Math.abs(y) ? 1 : -1
}
println list2

def list3 = ['abc', 'hello', 'a', 'java', 'groovy']
list3.sort {
    it.size()
}
println list3

def list4 = [6, -1, 4, 2, 0, -9]

//添加元素
println '---添加元素-----'
list4.add(10)
list4.leftShift(100)
list4 << 99
println list4
def plusList = list4 + 9
println plusList

//删除元素
println '---删除元素-----'
def size = list4.size()

list4.remove(size - 1)
println list4
list4.removeAt(size - 2)
println list4
list4.removeElement(10)
println list4
list4.removeAll {
    Math.abs(it) % 2 != 0
}
println list4

println list4 - [2, 4]


//列表的查找
println '---列表的查找-----'
def list5 = [6, -1, 4, 2, 0, -9]
println list5.find {
    it % 2 == 0
}
println list5.findAll() {
    it % 2 == 0
}.toListString()

println list5.any {
    it % 2 == 0
}

println list5.every {
    it % 2 == 0
}
//也可以通过闭包传入自定义的最值规则
println list5.max()
println list5.min()

println list5.count {
    it % 2 == 0
}

/**
 * map
 */
println '---map-----'
def colors = [red: 'ff0000', green: '00ff00', blue: '0000ff']
println colors['red']
println colors.green

//不能使用colors.class 获取map的class ，这种会误以为是获取key为class的value

colors.yellow = 'ffff00'
colors.complex = [a: 1, b: 2]
println colors.toMapString()

println '---map操作-----'
def students = [
        1: [number: '0001', name: 'bob', score: 55, sex: 'male'],
        2: [number: '0002', name: 'johnny', score: 62, sex: 'female'],
        3: [number: '0003', name: 'amy', score: 73, sex: 'female'],
        4: [number: '0004', name: 'cherry', score: 66, sex: 'male'],
]

//遍历
students.each {
    student -> println "each-------${student.key}====${student.value}"
}

students.eachWithIndex { student, index -> println "eachWithIndex----$index-----${student.key}====${student.value}" }

students.each {
    key, value -> println "each-------${key}====${value}"
}

students.eachWithIndex { key, value, index -> println "eachWithIndex----$index-----${key}====${value}" }

println students.find {
    student -> student.value.score > 60
}

println students.findAll {
    student -> student.value.score > 60
}.collect {
    it.value.name
}

println students.count {
    student -> student.value.score > 60
}

println students.groupBy {
    student -> student.value.score >= 60 ? '及格' : '不及格'
}

println students.sort {
    student1, student2 -> student1.value.score == student2.value.score ? 0 : student1.value.score < student2.value.score ? -1 : 1
}

/**
 * 范围
 */
println '---range-----'
def range1=1..10  //包含两边的极限值
println range1[0]
println range1.contains(10)
println range1.from
println range1.to

//遍历
range1.each{
    print it
}



